package org.jeecg.modules.message.controller;

import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.constant.CommonConstant;
import org.jeecg.common.system.util.JwtUtil;
import org.jeecg.common.util.RedisUtil;

import org.jeecg.modules.message.entity.SmsInterconnectAccount;
import org.jeecg.modules.message.service.ISmsInterconnectAccountService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;


/**
 * @author tomliu
 * @description: TODO
 * @date 2022/11/1721:30
 */
@RestController
@RequestMapping("/token")
@Slf4j
public class TokenController {

    @Resource
    ISmsInterconnectAccountService smsInterconnectAccountService;
    @Resource
    RedisUtil redisUtil;

    @PostMapping(value = "/getToken")
    public Result<String> send(@RequestParam(name = "clientId", required = true)String  clientId,@RequestParam(name = "secret", required = true) String secret) {
        SmsInterconnectAccount smsInterconnectAccount =  smsInterconnectAccountService.querybyClintIdAndSecret(clientId,secret);
        String token = JwtUtil.sign(String.valueOf(smsInterconnectAccount.getTenantId()),smsInterconnectAccount.getSecret());
        // 设置token缓存有效时间
        redisUtil.set(CommonConstant.PREFIX_USER_TOKEN + token, token);
        redisUtil.expire(CommonConstant.PREFIX_USER_TOKEN + token, JwtUtil.EXPIRE_TIME * 2 / 1000);
        return Result.OK(token);
    }

}
